package com.leather.common.result;

import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {

    private int code;

    private String errMsg;

    private T data;

    private Result() {

    }

    private Result(int code, String message) {
        this.code = code;
        this.errMsg = message;
    }

    private Result(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return this.code == CodeMsg.SUCCESS.getCode();
    }

    private Result(CodeMsg codeMsg) {
        this.code = codeMsg.getCode();
        this.errMsg = codeMsg.getMessage();
        data = null;
    }

    private Result(CodeMsg responseCode, T data) {
        this.code = responseCode.getCode();
        this.errMsg = responseCode.getMessage();
        this.data = data;
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(CodeMsg.SUCCESS, data);
    }

    public static <T> Result<T> error(CodeMsg responseCode) {
        return new Result<>(responseCode);
    }

    public static <T> Result<T> exception(int code, String message) {
        return new Result<>(code, message);
    }


}
